/**
 *	Protactica Script
 *	@author Gary Rios O'Hara
 */

(function($) {
/**
 *	jQuery background cover plugin
 *	@author Gary Rios O'Hara
 *	@param {Object} options The options object
 *	@param {Boolean} center Horizontally center the image
 *	@param {Boolean} middle Vertically center the image
 *	@param {Number} width Image width
 *	@param {Number} height Image height
 */
$.fn.coverbg = function(options) {
	options = $.extend({
		center: false,
		middle: false,
		width: 800,
		height: 600
	}, options);
	
	return this.each(function() {
		var $this = $(this),
		$container = $this.parent(),
		center = options.center,
		middle = options.middle,
		img_aspect_ratio = options.height / options.width;
		
		function resize() {
			var margin_top = "",
				margin_left = "",
				container_aspect_ratio = $container.height() / $container.width();
			if(img_aspect_ratio >= container_aspect_ratio) {
				$this.css({"width": "100%", "height": "auto"});
				if(middle) margin_top = ($container.height() - $this.height()) / 2;
			}
			else {
				$this.css({"width": "auto", "height": "100%"});
				if(center) margin_left = ($container.width() - $this.width()) / 2;
			}
			if(center || middle) $this.css({"margin-top": margin_top, "margin-left": margin_left});
		}
		
		resize();
		$(window).on("resize", resize);
	});
};
})(jQuery);


(function() {

// vars
var $window = $(window),
	$htmlBody = $("html, body"),
	$header = $("#header"),
	$nav = $("#nav"),
	$content = $("#content"),
	$section = $content.find("div.section"),
	currentIndex = 0,
	scrollTimeout,
	scrollHandler = function scrollHandler() {
		if(!$section.length) return;
		clearTimeout(scrollTimeout);
		scrollTimeout = setTimeout(function() {
			var index = -1,
				position = 9999;
			$window.off("scroll");
			$section.each(function(i) {
				var distance = Math.abs($window.scrollTop() - $(this).offset().top + $header.innerHeight());
				if(distance <= position) {
					position = distance;
					index = i;
				}
			});
			$nav.find("a").removeClass("active").eq(index).addClass("active");
			position = $section.eq(index).offset().top - $header.innerHeight();
			if(index === $section.length - 1) $window.on("scroll", scrollHandler);
			else {
				$htmlBody.animate({scrollTop: position}, {
					step: parallaxOffset,
					complete: function() { $window.on("scroll", scrollHandler); }
				});
			}
			currentIndex = index;
		}, 1000);
		parallaxOffset();
	},
	parallaxOffset = function parallaxOffset() {		
		$section.find(".offset").not(".disabled").each(function(index, element) {
			var offset = $(this).closest("div.section").offset().top - $header.innerHeight();
			offset = ($window.scrollTop() - offset) * -0.75;
			//offset = offset < 0 ? offset : 0;
			$(this).css("top", offset);
		});
	};

// event bindings
// window
$window.on({
	resize: function() {
		var height = $htmlBody.height() - $header.innerHeight();
		$content.css("padding-top", $header.innerHeight());
		$section.css("height", height);
		$window.trigger("scroll");
	},
	load: function() {
		$window.trigger("resize");
	},
	scroll: scrollHandler
});
// nav
$nav.on("click", "a", function() {
	var index = $(this).parent().index(),
		duration = Math.abs(index - currentIndex) * 1000,
		position = $section.eq(index).offset().top - $header.innerHeight();
	currentIndex = index;
	clearTimeout(scrollTimeout);
	$window.off("scroll");
	$nav.find("a").removeClass("active").eq(index).addClass("active");
	$htmlBody.stop().animate({scrollTop: position}, {
		step: parallaxOffset,
		duration: duration,
		complete: function() { $window.on("scroll", scrollHandler); }
	});
});
// content
$content.on("click", "div.section-item a", function() {
	var index = $(this).closest("div.section-item").index(),
		position = ((index - 1) * 100) + "%";
	$(this).closest("div.section-items").animate({left: position});
});

// init
$content.css("height", "auto");
$section.find(".section-item").children("img").coverbg({width: 1600, height: 895, center: true});

})();